var $tbody = $('tbody');
var $info_tmpl = $('#info_tmpl');
var totalPage = 1; // 总页数
var query = {};
query.pageNo = 1;
query.pageSize = 15;
$(function () {
    getInsuranceInfo();

    $(".addBtn").click(function(){
        //定义默认时间
        document.getElementsByClassName("selDate")[0].value = today();
        $('.addWindow').modal();
    });
    $(".exportBtn").click(function () {
        exportInfo();
    });

    resetPage();

    $('.modal').on('hidden.bs.modal', function () {
        resetObject();
    })

});

function search() {
    var jsonData = serializeObject($("#search form"));
    $.extend(true, query, jsonData);
    getInsuranceInfo();
}

function searchReset() {
    $("#search form input").val("");
    search();
}

function getInsuranceInfo(){
    $.ajax({
        url: '/hospital/commonReferral/list?column=roll_out_time&order=desc',
        data:query,
        type: 'get',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        cache: false,
        success: function(data) {
            var list = data.result.records;
            var html = "";
            if(list.length > 0){
                html = $info_tmpl.render(list);
            }else{
                html = "<tr><td colspan='11' align='center'>暂无数据</td></tr>";
            }
            $tbody.html(html);
            totalPage = data.result.pages;
            $("#totalCountHtml").html(data.result.total);
            $("#pageInforHtml").html(query.pageNo + "/" + totalPage);
            changePageButton();
        },
        error: function(xhr) {
        }
    });
}


function add(){
    var jsonData = serializeObject($("form.add"));
    $.ajax({
        url: '/hospital/commonReferral/add',
        data:JSON.stringify(jsonData),
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        cache: false,
        success: function(data) {
            if(data.success){
                layer.msg(data.message,{icon: 1});
                $('.addWindow').modal('hide');
                getInsuranceInfo();
            }
        },
        error: function(xhr) {
        }
    });
}

function edit(editid) {
    $('.editWindow').modal();
    $.ajax({
        url: '/hospital/commonReferral/queryById',
        data:{
            'id' : editid
        },
        type: 'get',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        cache: false,
        success: function(data) {
            var list = data.result;
            setObject(list);
        },
        error: function(xhr) {
        }
    });
}

function editInfo() {
    var jsonData = serializeObject($("form.edit"));
    $.ajax({
        url: '/hospital/commonReferral/edit',
        data:JSON.stringify(jsonData),
        type: 'put',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        cache: false,
        success: function(data) {
            if(data.success){
                layer.msg(data.message,{icon: 1});
                $('.editWindow').modal('hide');
                getInsuranceInfo();
            }
        },
        error: function(xhr) {
        }
    });
}

function deleteInfo(id){
    $("#confirmDeleteId").val(id);
    $('.delWindow').modal();
}

function confirmDelete() {
    var id = $("#confirmDeleteId").val();
    $.ajax({
        url: '/hospital/commonReferral/delete?id='+id,
        type: 'delete',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        cache: false,
        success: function(data) {
            if(data.success){
                layer.msg(data.message,{icon: 1});
                $('.delWindow').modal('hide');
                getInsuranceInfo();
            }
        },
        error: function(xhr) {
        }
    });
}

function exportInfo() {
    console.log("导出");
    window.location.href = "/hospital/commonReferral/exportXls" +
        "?pageNo="+ query.pageNo +"&pageSize="+ query.pageSize;
}

function today(){
    var today=new Date();
    var h=today.getFullYear(); //年
    var m=today.getMonth()+1; //月
    var d=today.getDate(); //日
    var hh=today.getHours();
    var mm=today.getMinutes();
    var ss=today.getSeconds();
    m= m<10?"0"+m:m;
    d= d<10?"0"+d:d;
    hh = hh < 10 ? "0" + hh:hh; //时
    mm = mm < 10 ? "0" +  mm:mm; //分
    ss = ss < 10 ? "0" + ss:ss; //秒
    return h+"-"+m+"-"+d;
}

/**
 *序列化form表单
 *@param  要序列化的表单的名称
 *@return 返回对象
 ***/
function serializeObject(form){
    var o = {};
    $.each(form.serializeArray(),function(index){
        if(this['name'].indexOf('like') != -1){
            o[this['name'].split('_')[0]] =  "*" + this['value'] + "*";
        }else{
            if(o[this['name']]){
                o[this['name']] = o[this['name']] + ","+this['value'];
            }else{
                o[this['name']] =  this['value'];
            }
        }
    });
    return o;
}

/**
 *
 * @param form
 */
function setObject(list) {
    for(var item in list){
        $editInput = $("input[name='"+ item +"'].edit");
        $edittextarea = $("textarea[name='"+ item +"'].edit");

        if($editInput.length == 1){
            $editInput.val(list[item]);
        }

        if($editInput.length == 2){
            $editInput.eq(list[item]-1).prop('checked',true);
        }

        if($editInput.length == 3){
            $editInput.eq(list[item]-1).prop('checked',true);
        }

        if($edittextarea.length == 1){
            $edittextarea.val(list[item]);
        }

    }
}

function resetObject() {
    $("input[type='text']").each(function () {
        $(this).val("");
    });
    $("input[type='radio']").each(function () {
        $(this).prop('checked',false);
    });
    $("textarea").each(function () {
        $(this).val("");
    });
}


function resetPage() {
    //首页
    $("#first_a").click(function(){
        query.pageNo = 1;
        if($(this).attr("name") == "first_enable"){
            $("#pageInput").val("");
            getInsuranceInfo();
        }
    });
    //上一页
    $("#prev_a").click(function(){
        if(query.pageNo == 1){
            layer.msg('已经是首页了！',{icon: 0});
        } else {
            query.pageNo = query.pageNo - 1;
            if($(this).attr("name") == "prev_enable"){
                $("#pageInput").val("");
                getInsuranceInfo();
            }
        }
    });
    //下一页
    $("#next_a").click(function(){
        if(query.pageNo == totalPage){
            layer.msg('已经是末页了！',{icon: 0});
        } else {
            query.pageNo = query.pageNo + 1;
            if($(this).attr("name") == "next_enable"){
                $("#pageInput").val("");
                getInsuranceInfo();
            }
        }
    });
    //末页
    $("#last_a").click(function(){
        query.pageNo = totalPage;
        if($(this).attr("name") == "last_enable"){
            $("#pageInput").val("");
            getInsuranceInfo();
        }
    });
    //输入页面序号，页面跳转
    $("#pageInputSubmit").click(function(){
        var pageInput = $("#pageInput").val();
        if(pageInput != undefined && pageInput != null && pageInput<=totalPage && pageInput>0){
            query.pageNo = parseInt(pageInput);
            getInsuranceInfo();
        } else if(pageInput<=0 || pageInput>totalPage){
            layer.msg('页面超限，请重新输入！',{icon: 2});
            $("#pageInput").val("");
        }
    });
}

//分页四个按钮的变化
function changePageButton(){
    if(totalPage == 1){
        $("#first_a").css({color:"#d0d0d0"});
        $("#first_a").attr("name","first_disable");
        $("#prev_a").css({color:"#d0d0d0"});
        $("#prev_a").attr("name","prev_disable");
        $("#next_a").css({color:"#d0d0d0"});
        $("#next_a").attr("name","next_disable");
        $("#last_a").css({color:"#d0d0d0"});
        $("#last_a").attr("name","last_disable");
    }else{
        if(query.pageNo == 1){
            $("#first_a").css({color:"#d0d0d0"});
            $("#first_a").attr("name","first_disable");
            $("#prev_a").css({color:"#d0d0d0"});
            $("#prev_a").attr("name","prev_disable");
            $("#next_a").css({color:"#3399d5"});
            $("#next_a").attr("name","next_enable");
            $("#last_a").css({color:"#3399d5"});
            $("#last_a").attr("name","last_enable");
        }else if(query.pageNo == totalPage){
            $("#first_a").css({color:"#3399d5"});
            $("#first_a").attr("name","first_enable");
            $("#prev_a").css({color:"#3399d5"});
            $("#prev_a").attr("name","prev_enable");
            $("#next_a").css({color:"#d0d0d0"});
            $("#next_a").attr("name","next_disable");
            $("#last_a").css({color:"#d0d0d0"});
            $("#last_a").attr("name","last_disable");
        }else{
            $("#first_a").css({color:"#3399d5"});
            $("#first_a").attr("name","first_enable");
            $("#prev_a").css({color:"#3399d5"});
            $("#prev_a").attr("name","prev_enable");
            $("#next_a").css({color:"#3399d5"});
            $("#next_a").attr("name","next_enable");
            $("#last_a").css({color:"#3399d5"});
            $("#last_a").attr("name","last_enable");
        }
    }
}

